﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Basic;
using Interface;

namespace Client
{
    /// <summary>
    /// UI相关的辅助
    /// </summary>
    public static class UIHelper
    {
        private static List<MaskableGraphic> s_MaskableGraphic = new List<MaskableGraphic>(4);
        /// <summary>
        /// 获取UI节点的raycast范围（世界坐标）
        /// </summary>
        /// <param name="transform"></param>
        /// <returns></returns>
        public static Rect GetChildRaycastWorldRect(RectTransform transform)
        {
            transform.GetComponentsInChildren<MaskableGraphic>(s_MaskableGraphic);
            if (s_MaskableGraphic.Count != 0)
            {
                float xMin = float.PositiveInfinity;
                float xMax = float.NegativeInfinity;
                float yMin = float.PositiveInfinity;
                float yMax = float.NegativeInfinity;
                foreach (var mg in s_MaskableGraphic)
                {
                    if (!mg.raycastTarget)
                    {
                        continue;
                    }

                    RectTransform trans = mg.rectTransform;
                    Vector3 tmpMin = trans.TransformPoint(trans.rect.min);// transform.InverseTransformPoint(trans.TransformPoint(trans.rect.min));
                    Vector3 tmpMax = trans.TransformPoint(trans.rect.max);// transform.InverseTransformPoint(trans.TransformPoint(trans.rect.max));

                    xMin = tmpMin.x < tmpMax.x ? (tmpMin.x < xMin ? tmpMin.x : xMin) : (tmpMax.x < xMin ? tmpMax.x : xMin);
                    xMax = tmpMin.x > tmpMax.x ? (tmpMin.x > xMax ? tmpMin.x : xMax) : (tmpMax.x > xMax ? tmpMax.x : xMax);
                    yMin = tmpMin.x < tmpMax.x ? (tmpMin.x < yMin ? tmpMin.x : yMin) : (tmpMax.x < yMin ? tmpMax.x : yMin);
                    yMax = tmpMin.y > tmpMax.y ? (tmpMin.y > xMax ? tmpMin.y : xMax) : (tmpMax.y > xMax ? tmpMax.y : xMax);
                }

                if (xMin != float.PositiveInfinity)
                {
                    return new Rect(xMin, yMin, xMax - xMin, yMax - yMin);
                }
            }
            return Rect.zero;
        }

        /// <summary>
        /// root节点到target节点的相对路径（两者之间必须存在路径）
        /// a/b/c
        /// </summary>
        /// <param name="target">检测节点</param>
        /// <param name="root">根节点</param>
        /// <returns></returns>
        public static string GetTransformRelativePath(Transform target, Transform root)
        {
            if (target == root)
            {
                return "";
            }

            string s = target.name;
            Transform parent = target.parent;
            while (parent != null)
            {
                if (parent == root)
                {
                    return s;
                }
                s = parent.name + "/" + s;
                parent = parent.parent;
            }
            if (parent != root)
            {
                throw new Exception(string.Format("{0} {1} 不是父子关系，无法计算相对路径", root.name, target.name));
            }
            return s;
        }
    }
}
